package cn.zt.controller;

import cn.zt.domain.dto.OrderDTO;
import cn.zt.domain.pojo.Result;
import cn.zt.service.IOrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private IOrderService orderService;

    //订票
    @PostMapping("/buy")
    public Result buy(@RequestBody List<OrderDTO> orderDTOList)
    {
        Result result = null;
        for (OrderDTO orderDTO : orderDTOList){
            System.out.println("用户" + orderDTO.getOrderUserId() + "正在为" + orderDTO.getUseUserId() + "预定"
                    + "航班" + orderDTO.getFlightId() + orderDTO.getClazz() + "舱位"  +
                    orderDTO.getSeatId() + "号座" + orderDTO.getTickets() + "张票");
            if (orderDTO.getOrderUserId() == null){
                return Result.fail("下单用户ID不能为空");
            }
            if (orderDTO.getUseUserId() == null){
                return Result.fail("使用用户ID不能为空");
            }
            System.out.println("用户" + orderDTO.getOrderUserId() + "正在为" + orderDTO.getUseUserId() + "预定"
                    + "航班" + orderDTO.getFlightId() + orderDTO.getClazz() + "舱位"  +
                    orderDTO.getSeatId() + "号座" + orderDTO.getTickets() + "张票");
            System.out.println();
            result = orderService.buy(orderDTO);
        }

        return result;
    }

    //退订
    @GetMapping("/cancel")
    public Result cancel(String orderId)
    {
        System.out.println("用户正在退订" + orderId);
        if (orderId == null) {
            System.out.println("订单ID不能为空");
            return Result.fail("订单ID不能为空");
        }
        Long id = Long.parseLong(orderId);
        System.out.println("用户正在退订Long:" + id);

        return orderService.cancel(id);
    }

    //同意候补
    @PostMapping("/wait")
    public Result wait(@RequestBody List<OrderDTO> orderDTOList){

        Result result = null;
        for (OrderDTO orderDTO : orderDTOList){
            System.out.println("用户" + orderDTO.getOrderUserId() + "正在为" + orderDTO.getUseUserId() + "候补"
                    + "航班" + orderDTO.getFlightId() + orderDTO.getClazz() + "舱位"  +
                    orderDTO.getSeatId() + "号座" + orderDTO.getTickets() + "张票");
            result = orderService.waitForFlight(orderDTO);
        }
        return result;
    }
}
